const url = `http://127.0.0.1:4000`
import axios from "axios";
// 创建axios实例
const http = axios.create({
    baseURL: url,
    timeout: 5000,
    headers: {
        token: localStorage.getItem("token")
    }
})
// 封装get请求
const get = (url: string, data = {}) => {
    return http.get(
        `${url}`, {
        params: data,
        headers: {
            token: localStorage.getItem("token")
        }
    }
    )
        .then(response => response.data )
}

// 封装post请求
const post = (url: string, data = {}) => {
    return http.post(
        `${url}`,
        data,
        {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                token: localStorage.getItem("token")
            }
        }
    )
    .then(response =>  response.data )
}

// 封装提交图片请求
const upload = (url: string, data = {}) => {
    return http.post(`${url}`, data, {
        headers: {
            'Content-Type': 'multipart/form-data',
            token: localStorage.getItem("token")
        }
    })
        .then(response => response.data )
}

export default {
    url,
    get,
    post,
    upload
}